﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EmoneyWeiXinBLL;
using EmoneyWeiXinCommon;
using EmoneyWeiXinEntity;
using EmoneyWeiXin.Filter;
namespace EmoneyWeiXin.Controllers
{
    public class MenuController : Controller
    {
        //
        // GET: /Menu/
        [LoginAuthorizeAttribute]
        public ActionResult Index()
        {
            return View();
        }


        public ActionResult MenuManager()
        {
            ViewBag.leftid = int.Parse(Request["leftid"]);
            var ds = MenuBLL.getMenuType();
            if (ds != null && ds.Tables.Count > 0)
            {
                ViewBag.menutype = ds.Tables[0];
            }
            else
            {
                ViewBag.menutype = null;
            }
            return View();
        }
        ////查询一级菜单
        //public string getMenuParents()
        //{
        //    var weixinid = int.Parse(Request["weixinid"]);
        //    var obj = MenuBLL.getMenuParentList(weixinid);
        //    return JsonConvert.SerializeObject(obj);
        //}
        ////查询二级菜单
        //public string getMenuChildrens()
        //{
        //    var id = int.Parse(Request["id"]);
        //    var weixinid = int.Parse(Request["weixinid"]);
        //    var obj = MenuBLL.getMenuChildrenList(id,weixinid);
        //    return JsonConvert.SerializeObject(obj);
        //}

        //查询菜单
        public string GetMenuParentAndChildren()
        {
            var pjson = "";
            var cjson = "";
            var weixinid = int.Parse(Request["weixinid"]);
            var obj = MenuBLL.getMenuParentAndChildren(weixinid);
            if (obj != null && obj.Tables.Count > 0)
            {
                var ptable = obj.Tables[0];
                var ctable = obj.Tables[1];
                return JsonTools.ToJson(new { p = ptable, c = ctable });
            }
            return "";

        }

        //根据id查询
        public string GetMenuByID()
        {
            var id = int.Parse(Request["id"]);
            var obj = MenuBLL.getMenuListByID(id);
            var obj2 = MenuBLL.getMenu_NewsReport_ListByID(id);
            return JsonTools.ToJson(new { obj = obj, obj2 = obj2 });
        }


        //编辑菜单
        [ValidateInput(false)]
        public string UpdateMenu()
        {
            int id = int.Parse(Request["id"]);

            var obj = new MenuEntity();
            if (id > 0)
                obj = MenuBLL.getMenuListByID(id);
            obj.id = int.Parse(Request["id"]);
            obj.weixinuserid = int.Parse(Request["weixinid"]);
            obj.title = Request["txtTitle"];//菜单名称
            //obj.Author = Request["tx"];
            obj.everyKey = Request["txtMenuKeyWord"];//菜单关键字
            obj.KeyUrl = Request["txtMenuKeyWord"].Trim();//菜单关键字
            obj.Keyword = Request["txtSubscribeKeyWord"].Trim();//订阅关键字
            obj.sort = Request["txtOrder"];//排序号
            obj.hisId = int.Parse((Request["txtParentID"] == null || Request["txtParentID"] == "") ? "0" : Request["txtParentID"]);//父菜单id
            obj.DayType = int.Parse(Request["rdPushRate"]);// 推送频率
            obj.thday = Request["cbDayType"];//周一1,2,3,4,5,6,7
            obj.starttime = Request["txtStartTime"];//时间范围选择--
            obj.endtime = Request["txtEndTime"];//时间范围选择--
            obj.jgtimeStr = Request["txtPushTime"];//推送时间点
            obj.isThread = int.Parse(Request["rdPush"]);//是否主动推送
            obj.contentym = Request["txtHeaderContent"]; //固定页头
            obj.contentyj = Request["txtFootContent"]; //固定页脚
            //obj.isvalid = int.Parse(Request["rdValid"]);   //1是否有效
            obj.isvalid = 1;   //1是否有效
            obj.PubStatus = int.Parse(Request["rdDefaultSend"]);//栏目是否默认发布
            obj.mtype = int.Parse(Request["drpMenuType"]);//菜单类型
            obj.linkUrl = Request["txtLinkUrl"];//跳转到网页的URL地址---
            obj.isAction = int.Parse(Request["rdAction"]);//设置动作
            obj.isSend = int.Parse(Request["rdActionType"]);//1发送信息;0跳转到网页
            obj.sendMsg = Request["txtSendMsg"];//发送文本消息的消息内容
            obj.isJianQuan = int.Parse(Request["isJianQuan"]);
            obj.createtime = DateTime.Now;
            obj.updatetime = DateTime.Now;
            //obj.newsid = int.Parse(Request["newsid"]);
            obj.isweixinMemu = Convert.ToInt32(Request["isweixinMemu"]);
            var newsreportids = Request["newsreportids"];
            var ir = MenuBLL.UpdateMenu(obj, newsreportids.Trim('^'));
            if (ir > 0)
            {
                return "1";
            }
            else
            {
                return "";
            }
        }
        //删除菜单
        public string DeleteMenu()
        {
            var id = int.Parse(Request["id"]);
            var ir = MenuBLL.DeleteMenu(id);
            if (ir > 0)
            {
                return "1";
            }
            else
            {
                return "";
            }
        }
        //查询菜单类型列表
        //public string GetMenuTypeList()
        //{
        //    var obj = MenuBLL.getMenuType();
        //    return JsonConvert.SerializeObject(obj);
        //}
        //更新微信菜单到微信服务器上方法
        public string UpdateWXMenu()
        {
            var weixinid = int.Parse(Request["weixinid"]);
            var ir = MenuBLL.UpdateWxMenu(weixinid);
            if (ir == 0)
            {
                return "1";
            }
            else
            {
                return "0";
            }
        }
    }
}
